//new_element=document.createElement("script");
//new_element.setAttribute("type","text/javascript");
//new_element.setAttribute("src","jquery.js");
//document.body.appendChild(new_element);

function mark_tag(marked_tag_id,type,is_login)
{
	//alert(marked_tag_id+","+type+","+is_login);
	if(!is_login)
    	return false;
	
    if(type=='关注')
        type='mark';
    else if(type=='取消关注')
        type='cancel_mark';
    
	var url=BASE_HOSTPATH+"/dr-includes/dr-ajax-mark-tag.php";
	var xmlhttp=false;
	if(window.ActiveXObject)
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	else if(window.XMLHttpRequest)
		xmlhttp=new XMLHttpRequest();
	if(!xmlhttp)
	{
		alert("不能创建XMLHTTP实例");
		return false;
	}

	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			var msg=xmlhttp.responseText;
			if(msg==1)
			{
				if(type=='mark')
			    {
					$(document).ready(function(){
						$("input."+"tag-"+marked_tag_id).val('取消关注');
						});
			       
			    }
			    else if(type=='cancel_mark')
			    {
			    	$(document).ready(function(){
						$("input."+"tag-"+marked_tag_id).val('关注');
						});
			    }
    		}
			else
    			alert(msg);
			
		}
	}

	result="marked_tag_id="+marked_tag_id+"&mark_type="+type;
	result+="&nocache="+new Date().getTime();
	//alert(result);
	xmlhttp.send(result);
}

function search_tag(match_str,is_login)
{
	if(match_str.length<1)
	{
		document.getElementById("tags").style.display='';
		document.getElementById("tags-search").style.display='none';
		return false;
	}
	else
	{
		document.getElementById("tags").style.display='none';
		document.getElementById("tags-search").style.display='';
		
	}
	
	var url=BASE_HOSTPATH+"/dr-includes/dr-ajax-search-tag.php";
	var xmlhttp=false;
	if(window.ActiveXObject)
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	else if(window.XMLHttpRequest)
		xmlhttp=new XMLHttpRequest();
	if(!xmlhttp)
	{
		alert("不能创建XMLHTTP实例");
		return false;
	}

	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			var msg=xmlhttp.responseText;
			//alert(msg);
			if(msg=='0')
			{
				document.getElementById("tags").style.display='';
				return false;
			}
			else
			{
				var tag=eval("("+msg+")");
				var tags=tag['tags'];
				var input='<table>';
				input+='<tr><th>标签</th><th>数量</th></tr>';
				for(i=0;i<tags.length;i++)
				{
					input+='<div id="tag-summary-'+tags[i].tag_id+'" class="tag-summary">';
					input+='<div class="tag">';
					input+='<tr>';
					input+='<td><a href="'+BASE_HOSTPATH+'dr-includes/dr-page-question-list.php?tid='+tags[i].tag_id+'" title="查看tag相关问题">'+tags[i].title+'</a></td>';
					input+='<td>'+tags[i].question_num+'</td>';
					input+='<td><div class="taglisten">';
					if(is_login)
					{
						if(tags[i].is_marked==true)
							input+='<span><input id="tag-search-'+tags[i].tag_id+'" class="tag-'+tags[i].tag_id+'" type="button" value="取消关注" onclick="mark_tag('+tags[i].tag_id+',value,'+is_login+')" /></span>';
						else
							input+='<span><input id="tag-search-'+tags[i].tag_id+'" class="tag-'+tags[i].tag_id+'" type="button" value="关注" onclick="mark_tag('+tags[i].tag_id+',value,'+is_login+')" /></span>';
					}
					input+='</div></td></tr></div></div>';
					
				}
				
				input+='</table>';
				document.getElementById("tags-search").innerHTML=input;
				
			  }
		  }
	
	}

	result="match_str="+match_str;
	result+="&nocache="+new Date().getTime();

	xmlhttp.send(result);
}